package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.DialogCatAdapter;
import com.dlc.felear.xiaoensale.adapter.EmnuListtemAdapter;
import com.dlc.felear.xiaoensale.adapter.LeftMenuCatAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.dao.CombineData;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.Operator;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.listener.TextWatcherAdapter;
import com.itdlc.android.library.utils.NetStateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 炒菜菜谱显示页面
 */
public class CookingMenuActivity extends BaseActivity {
    @BindView(R.id.e_gv)
    GridView gv;
    @BindView(R.id.c_lv)
    ListView lv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private CookingMenuDao cookingMenuDao;
    private EmnuListtemAdapter adapter;
    private CookingMenu cookingMenu;
    private TopbarHelper topbarHelper;
    private LeftMenuCatAdapter dialogCatAdapter;
    private CookingMenuMangerHelper cookingMenuMangerHelper;
    private String deviceType = "M400";
    private String catName = "全部";
    private HashMap<String, Boolean> hashMapLocal = new HashMap<>();
    private OperatorDao operatorDao;


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_menu_cooking;
    }

    @Override
    protected void initialView() {
        deviceType = Session.getSession().getString(Constant.D_GT_TYPE, "M400");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(CcLauncherActivity.class);
        topbarHelper.setTitle("菜谱列表");
        operatorDao = new OperatorDao(this);
        adapter = new EmnuListtemAdapter(this);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    cookingMenu = adapter.getData().get(position);
                    if (TextUtils.equals(cookingMenu.getMachineModoel(), deviceType)) {
                        Intent intent = new Intent(CookingMenuActivity.this, EmnuInfoActivity.class);
                        intent.putExtra("id", cookingMenu.getId());
                        startActivity(intent);
                    } else {
                        showLongToast("该菜谱设备型号与该机器不符(" + cookingMenu.getMachineModoel() + "_" + deviceType + ")");
                    }
                } catch (Exception e) {
                    showShortToast("数据异常:" + e.getMessage());
                }
            }
        });
    }


    @Override
    protected void initData() {
        cookingMenuDao = new CookingMenuDao(this);
        cookingMenuMangerHelper = CookingMenuMangerHelper.getInstance(this, cookingMenuDao);
        initDaoData();
        dialogCatAdapter = new LeftMenuCatAdapter(this);
        lv.setAdapter(dialogCatAdapter);
        dialogCatAdapter.setItems(cookingMenuMangerHelper.getCatListNames());
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialogCatAdapter.setSelectitem(position);
                handleCat(dialogCatAdapter.getItem(position));
            }
        });
    }

    private void initDaoData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "data=" + JsonHelper.toJson(cookingMenuDao.queryForAll()));
        adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(getString(R.string.quanbu), deviceType, Constant.MENU_ALL)));
    }

    @OnClick({R.id.btn_ss})
    public void clickView(View view) {
        InputMethodManagerUtil.hideKeyboard(view);
        clickItem("ss");
    }

    private void clickItem(String tag) {
        if (!TextUtils.equals(tag, "ss")) {
            adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(tag, deviceType, Constant.MENU_ALL)));
            return;
        }
        etSeach.setText(etSeach.getText().toString().trim().toUpperCase());
        showLikeName(etSeach.getText().toString().trim().toUpperCase());
    }

    private void showLikeName(String key) {
        List<CookingMenu> tempList = checkData(cookingMenuMangerHelper.getCatMenuList(getString(R.string.quanbu), deviceType, Constant.MENU_ALL));
        if (TextUtils.isEmpty(key)) {
            adapter.setItems(tempList);
            return;
        }
        ArrayList<CookingMenu> ttt = new ArrayList<>();
        for (CookingMenu cookingMenu : tempList) {
            if (cookingMenu.getName().contains(key)) {
                ttt.add(cookingMenu);
            }
        }
        adapter.setItems(ttt);
    }


    private void handleCat(String catName) {
        this.catName = catName;
        if (!NetStateUtils.isNetworkConnected(this) && TextUtils.equals(catName, getString(R.string.jinricaipin))) {
            showShortToast("网络未连接，请检查网络");
            return;
        }
        adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(catName, deviceType, Constant.MENU_ALL)));
    }

    private List<CookingMenu> checkData(List<CookingMenu> data) {
        if (data == null || data.isEmpty()) {
            return new ArrayList<>();
        }
        Log.e(TAG, "");
        Map<String, CookingMenu> tempMap = new HashMap<>();
        try {
            for (CookingMenu cookingMenu : data) {
                boolean namerr = cookingMenu.getName().contains(cookingMenuMangerHelper.changeValue(catName));
                Log.e(TAG, "name:" + cookingMenu.getName() + " namerr:" + namerr + " catName:" + cookingMenuMangerHelper.changeValue(catName));
                if (TextUtils.equals(catName, getString(R.string.quanbu)) || TextUtils.equals(catName, getString(R.string.jinricaipin))) {
                    namerr = true;
                }
                if (Integer.parseInt(cookingMenu.getSpecification()) <= 1 && namerr && checkHasLocalData(cookingMenu)) {
                    tempMap.put(cookingMenu.getName() + cookingMenu.getSpecification(), cookingMenu);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "checkData Exception:" + e.getMessage());
        }
        return new ArrayList<>(tempMap.values());
    }

    private boolean checkHasLocalData(CookingMenu cookingMenu) {
        if (hashMapLocal.containsKey(cookingMenu.getId())) {
            return hashMapLocal.get(cookingMenu.getId());
        }
        List<Operator> tempList = operatorDao.queryForAllByMID(cookingMenu.getId());
        if (tempList == null || tempList.isEmpty()) {
            hashMapLocal.put(cookingMenu.getId(), false);
            return false;
        }
        hashMapLocal.put(cookingMenu.getId(), true);
        return true;
    }

}